#
# pH+
#
require 'json'

module Ph

  class Data
    
    attr_accessor :session
    attr_accessor :default_action
    
    def initialize(session)
      @session = session
      @session.log "Data " + self.class.name + " loaded."
    end   
    
    # Calls Method 'a' if it exists.
    def action (a)
      
      if self.respond_to?(a) then
        @session.log "Executing Method: " + a
        self.method(a).call
      elsif @default_action then
        @default_action.call
      else
        @session.log "WARNING: Invalid method: " + a
      end
      
    end
    
    # Handles the data request and runs the appropiate method.
    def request()
      
      self.action(@session.action) if @session.action
      @session.close
      
    end
    
  end

end
